$(function() {	
	//下拉初始化
	InitParentCate();
	//父分类切换
	$("#parentId").change(function(){		
		var parentId=$(this).val();
		changeChildKv(parentId);
	});
	
	gridObj = $.fn.bsgrid.init('g_tabYeh', {
		ajaxType : 'get',
		url :  server_url+'/rest/productSpec/ProductSpecList.do?ran=' + Math.random() + '&token=' + token,
		autoLoad : true,
		pageSizeSelect : true,
		stripeRows : true,
		pageSize : 10,
		displayBlankRows : false,
		isProcessLockScreen : true,
		rowSelectedColor : false,
		showPageToolbar:true,
		rowHoverColor:true
	});
});


function operate(record, rowIndex, colIndex, options) {	
	if(gridObj.getRecordIndexValue(record, 'type')==1)
	{
		return '<a href="javascript:EditProductSpec(\'' + gridObj.getRecordIndexValue(record, 'specId') + '\');">编辑</a>';
	}else{
		return '<a href="javascript:AddChildProductSpec(\'' + gridObj.getRecordIndexValue(record, 'cateId') + '\',\'' + gridObj.getRecordIndexValue(record, 'specId') + '\',\''+  gridObj.getRecordIndexValue(record, 'specName')+'\');">维护属性值</a><a href="javascript:EditProductSpec(\'' + gridObj.getRecordIndexValue(record, 'specId') + '\');">编辑</a>';
	}
	
  
}

//初始化下拉列表
function InitParentCate()
{
	
	$.ajax({
		url:server_url+'/rest/productSpec/selectParentSpec.do?ran='+Math.random()+"&token="+token,
		type:'get',
		success:function(data){
			if(data.retCode=="0000"){
				for(var i=0;i<data.data.length;i++){
					$("#parentId").append("<option value='"+data.data[i].cateId+"'>"+data.data[i].cateName+"</option>");
				}
			}
			else{
				easyDialog.open({
					  container : {
						  header : '提示',
					      content : data.retMsg
					  },
					  autoClose : 2000
					});				
			} 			
		}
	});	
}

//切换子级分类
function changeChildKv(parentId){
	$.ajax({
		url:server_url+'/rest/productCate/selectChildCateKv.do?ran='+Math.random()+"&token="+token,
		type:'post',
		data:{cateId:parentId},
		success:function(data){
			if(data.retCode=="0000"){
				for(var i=0;i<data.data.length;i++){
					$("#cateId").append("<option value='"+data.data[i].cateId+"'>"+data.data[i].cateName+"</option>");
				}	
             }
			else{
				easyDialog.open({
					  container : {
						  header : '提示',
					    content : data.retMsg
					  },
					  autoClose : 2000
					});			
			}
		}
	});
}


//数据表格参数查询
function doSearch() {
	searchParames = $('#myform').serialize();	
	if ($('#specName').val() == '') {
		gridObj.search('');
	} else {
		gridObj.search(searchParames);
	}	
	//console.log(searchParames);
}

//添加
function toProductSpecAdd(){

	var url="goproductSpecAdd.do?ran="+Math.random();
	tl_config.loadToWorkArea(url);
}

//维护属性值
function AddChildProductSpec(cateId,specId,specName) {
	//alert(id);
	var url='ProductSpecValueList.do?cateId='+cateId+'&specId='+specId+'&specName='+specName;
	tl_config.loadToWorkArea(url);
	$.ajax({
		url:server_url+'/rest/ProductValue/selectValueById.do?ran='+Math.random()+"&token="+token,
		type:'post',
		data:{specId:specId},
		success:function(data){
			if(data.retCode=="0000"){
				for(var i=0;i<data.data.length;i++){
					$("#table").append(""+
							'<tr >' +
 				    		'<td><input type="hidden" id="specValueId" value="'+data.data[i].specValueId+'" name="specValueId"><input type="text"  class="t1" value="'+data.data[i].specValueName+'" id="specValueName" name="specValueName" maxlength="30" width="160" readonly="readonly" /></td>'+
 				    		'<td><input type="text"  class="t1" value="'+data.data[i].isValidate+'" id="isValidate" name="isValidate" width="120"/></td>'+
 				    		'<td><input type="text"  class="t1" value="'+data.data[i].sortNum+'" id="sortNum" name="sortNum" maxlength="30" width="120" readonly="readonly" /></td>'+
 				    		'<td><input type="text"  class="t1"value="'+data.data[i].remark+'" id="remark" name="remark" maxlength="30" width="280" readonly="readonly" /></td>'+
 				    		//'<input name="key" id="key'+index+'" type="hidden" value="'+key+'" maxlength="100"/>' +
 				    		'<td><a href="javascript:delSpecValue('+data.data[i].specValueId+');" class="z_delpost">删除</a></td>'+
 				    	"</tr>");
				}	
             }
			else{
				easyDialog.open({
					  container : {
						  header : '提示',
					    content : data.retMsg
					  },
					  autoClose : 2000
					});			
			}
		}
	});
}
//单行删除
function delSpecValue(specValueId){
	$.ajax({
		url : server_url+'/rest/ProductValue/ProductSpecValueDel.do',
		type : 'post',
		dataType : 'json',
		data : {specValueId:specValueId},
		success : function(data) {
			if (data.retCode = "0000") {							
				var url="productSpecList.do?ran="+Math.random();
				tl_config.loadToWorkArea(url);
			} else{
				easyDialog.open({
					container:{header:'温馨提示',content:data.retMsg},
					autoClose : 2000
				});
			}  
		}
	});
}
// 编辑
function EditProductSpec(specId) {
	//alert(id);
	var url='goproductSpecEdit.do?specId='+specId;
	tl_config.loadToWorkArea(url);
}

//获取checkbox选中删除
function delCheckedIds() {
	// values are array
	var ids = gridObj.getCheckedValues('specId').toString();
	if (ids == "") {
		easyDialog.open({
			container:{
				header:'温馨提示',
				content:"请选择要操作的记录！"
			},
			autoClose : 2000
		});
	} else {
		easyDialog.open({
			  container : {
				header:'温馨提示',
			    content : '您确定要删除吗？',
			    yesFn : function(){
			    	$.ajax({
						url : server_url+'/rest/productSpec/productSpecDel.do',
						type : 'post',
						dataType : 'json',
						data : {
							updateUser:username,
							ids : ids,
							token:token
						},
						success : function(data) {
							if (data.retCode = "0000") {								
								var url="productSpecList.do?ran="+Math.random();
								tl_config.loadToWorkArea(url);
							} else{
								easyDialog.open({
									container:{header:'温馨提示',content:data.retMsg},
									autoClose : 2000
								});
							}  
						}
					});
			    },
			    noFn : true
			  }
			});
		
	}
}